package com.zql.LeetCode;

/**
 * Created By ShuHua on 2021/8/21.
 */
public class Difference_370 {
    // 差分数组
    private int[] diff;

    public Difference_370(int[] nums){
        diff = new int[nums.length];
        diff[0] = nums[0];
        for(int i=1;i<nums.length;i++){
            diff[i]=nums[i]-nums[i-1];
        }
    }

    public void increment(int i, int j, int val) {
        diff[i]=diff[i]+val;
        //这里要注意判断j是否在数组的长度范围内
        if(j+1<diff.length){
            diff[j+1]=diff[j+1]-val;
        }
    }

    public int[] result() {
        int[] nums = new int[diff.length];
        nums[0] = diff[0];
        for(int i=1;i<nums.length;i++){
            nums[i]=diff[i]+nums[i-1];
        }
        return nums;

    }
}
